import util from '@/libs/util';
import Cookies from 'js-cookie';
import sondata from './sondata.vue';

export default {
    name: 'exportable-table',
    components: { sondata },
    data () {
        return {
            columns: [{
                type: 'expand',
                width: 60,
                render: (h, params) => {
                    return h(sondata, {
                        props: {
                            parentid: params.row.id
                        }
                    });
                }
            }, {
                title: '序号',
                key: 'rownum',
                align: 'center',
                width: 60,
                render: (h, params) => {
                    return h('div', {}, ((this.pageIndex - 1) * this.pageSize + params.index + 1));
                }
            }, {
                title: '字典名称',
                key: 'name'
            }, {
                title: '描述',
                key: 'description'
            },{
                title: '状态',
                key: 'status',
                render: (h, params) => {
                    let texts = '';
                    if (params.row.status === 1) {
                        texts = "在用"
                    } else if (params.row.status === 2) {
                        texts = "停用"
                    }
                    return h('div', {}, texts)
                }
            }, {
                title: '排序',
                key: 'orders'
            }, {
                title: '操作',
                key: 'action',
                align: 'center',
                render: (h, params) => {
                    return h('div', [
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    this.showAdd(params.row);
                                }
                            }
                        }, '添加字典'),
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    this.showEdit(params.row);
                                }
                            }
                        }, '修改'),
                        h('Button', {
                            props: {
                                type: 'error',
                                size: 'small'
                            },
                            style: {
                                display: (eval('(' + Cookies.get('baseUser') + ')').loginid === 'admin' ? 'inline' : 'none')
                            },
                            on: {
                                click: () => {
                                    this.delete(params.row.id);
                                }
                            }
                        }, '删除')
                    ]);
                }
            }],
            data: [],
            formData: {},
            total: 0,
            pageIndex: 1,
            pageSize: 10,
            formDataRules: {// 添加班级表单验证规则
                name: [
                    { required: true, message: '字典名称不能为空', trigger: 'blur',type: 'string'}
                ],
                code: [
                    { required: true, message: '字典代码', trigger: 'blur', type: 'string'}
                ]
            },
            viewModal: false,
            isRead: false
        };
    },
    mounted () {
        this.loadList();
    },
    methods: {
        loadList () {
            util.ajax.post('/base/datadictionary/list', util.toFormData({
                page: this.pageIndex,
                size: this.pageSize
            })).then(res => {
                this.data = res.data.data.list;
                this.total = res.data.data.total;
            });
        },
        showAdd (obj) {
            this.formData = {
                parentId: (obj.id ? obj.id : ''),
                status: 1,
                code: obj.code
            };
            this.viewModal = true;
            this.isRead = false;
            if (obj.id) {
                this.isRead = true;
            }
        },
        showEdit (obj) {
            let formData = {};
            for (var key in obj) {
                formData[key] = obj[key];
            }
            this.formData = formData;
            this.viewModal = true;
            this.isRead = true;
        },
        save () {
            let formData = this.formData;
            // if (!this.checkForm(formData)) return false;
            this.$refs['formData'].validate((valid) => { // 表单验证
                if (valid) {
                    util.ajax.post('/base/datadictionary/save', util.toFormData(formData)).then(res => {
                        if (res.data.message === 'SUCCESS') {
                            this.viewModal = false;
                            this.$Message.success('保存成功！');
                            this.loadList();
                        } else {
                            this.$Message.warning(res.data.message || '未知错误！');
                        }
                    });
                } else {
                    this.$Message.warning('请检查输入项！');
                }
            });
        },
        delete (id) {
            this.$Modal.confirm({
                title: '提示',
                content: '<p>删除会同时删除所有子字典，确定删除？</p>',
                onOk: () => {
                    util.ajax.post('/base/datadictionary/delete', util.toFormData({
                        id: id
                    })).then(res => {
                        if (res.data.message === 'SUCCESS') {
                            this.$Message.success('删除成功！');
                            this.loadList();
                        }
                    });
                },
                onCancel: () => {
                }
            });
        },
        checkForm (obj) {
            if (!(obj.name) || obj.name === '' || obj.name === null) {
                this.$Modal.error({
                    title: '警告',
                    content: '字典名称不能为空'
                });
                return false;
            }
            if (!(obj.status) || obj.status === '' || obj.status === null) {
                this.$Modal.error({
                    title: '警告',
                    content: '请选择状态'
                });
                return false;
            }
            return true;
        }
    }
};